﻿//  --------------------------------
//  Copyright (c) PK Logix LLC. All rights reserved.
//  This source code is made available under the terms of the GNU General Public License version 2 (GPLv2)
//  http://www.codeplex.com/mvcclubsite/license
//  ---------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MVCClubSite.IDAL;
using MVCClubSite.Model;

namespace MVCClubSite.LinqToSQLDAL
{
    /// <summary>
    /// Linq To SQL Albums DAL
    /// </summary>
    public class Albums : IAlbums
    {
       DB _db;

        /// <summary>
        /// Default constructor
        /// </summary>
        public Albums()
        {
            _db = new DB();
        }

        /// <summary>
        /// Constructor used to pass in a datacontext
        /// </summary>
        /// <param name="db">datacontext for the DAL to use</param>
        public Albums(DB db)
        {
            _db = db;
            if (_db == null)
                throw new InvalidOperationException("DB cannot be null.");
        }

        /// <summary>
        /// Method to get all albums
        /// </summary>
        /// <returns>A list of albums</returns>
        public IQueryable<Album> GetAllAlbums()
        {
            return from a in _db.cs_Albums
                   select new Album 
                   {
                       ID = a.AlbumID,
                       Title = a.Title,
                       Description = a.Description,
                       IsPrivate = a.IsPrivate,
                       ImageCount = _db.cs_Images.Where(i => i.AlbumID == a.AlbumID).Count()
                   };

        }

        /// <summary>
        /// Method to get public albums
        /// </summary>
        /// <returns>A list of albums</returns>
        public IQueryable<Album> GetPublicAlbums()
        {
            return from a in _db.cs_Albums
                         where a.IsPrivate == false
                         select new Album
                         {
                             ID = a.AlbumID,
                            Title = a.Title,
                            Description = a.Description,
                            IsPrivate = a.IsPrivate,
                             ImageCount = _db.cs_Images.Where(i => i.AlbumID == a.AlbumID).Count()
                         };
        }

        /// <summary>
        /// Method to get an ablum by and album ID
        /// </summary>
        /// <param name="id">ID of the album to return</param>
        /// <returns>An album business entity</returns>
        public Album GetAlbumByID(int id)
        {
            return (from a in _db.cs_Albums
                    where a.AlbumID == id
                    select new Album
                    {
                        ID = a.AlbumID,
                        Title = a.Title,
                        Description = a.Description,
                        IsPrivate = a.IsPrivate,
                        ImageCount = _db.cs_Images.Where(i => i.AlbumID == a.AlbumID).Count()
                    }).SingleOrDefault();
        }

        /// <summary>
        /// Method to insert or update an album
        /// </summary>
        /// <param name="album">Album to insert or update</param>
        public void Save(Album album)
        {
            using (DB db = new DB())
            {
                cs_Album dbAlbum = db.cs_Albums.Where(a => a.AlbumID == album.ID).SingleOrDefault();
                bool isNew = false;
                if (dbAlbum == null)
                {
                    dbAlbum = new cs_Album();
                    isNew = true;
                }

                dbAlbum.Title = album.Title;
                dbAlbum.Description = album.Description;
                dbAlbum.IsPrivate = album.IsPrivate;

                if (isNew)
                    db.cs_Albums.InsertOnSubmit(dbAlbum);

                db.SubmitChanges();
            }
        }

        /// <summary>
        /// Method to delete an album
        /// </summary>
        /// <param name="id">ID of the album to delete</param>
        public void Delete(int id)
        {
            cs_Album dbAlbum = _db.cs_Albums.Where(a => a.AlbumID == id).SingleOrDefault();

            if (dbAlbum != null)
            {
                _db.cs_Albums.DeleteOnSubmit(dbAlbum);
                _db.SubmitChanges();
            }
        }
    }
}
